﻿<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Checkout extends BIT_Controller
{
	public $order_msg;
	
	function __construct() {
		parent::__construct();

		$this->header_views = array('common/header');
		$this->footer_views = array('common/footer');
		$this->load->library('checkout_handler');

		$this->load_standards();

		$data['loggedin'] = $this->tank_auth->is_logged_in();
		$this->view_data['widgets'] = array(
			$this->load->view('widgets/loginwidget', $data, TRUE)
		);
	}
	
	public function index() {
		$user_data = array(
			'fnamn' => '',
			'enamn' => '',
			'email' => $this->tank_auth->get_email(),
			'telefon' => '',
			'gadress' => '',
			'postnr' => '',
			'postort' => ''
		);
		if( $this->tank_auth->is_logged_in())
			$user_profile = $this->obj_to_array($this->users->get_profile( $this->tank_auth->get_user_id() ));
		else
			$user_profile = array();

		// Merge userdata and userprofile 
		$this->view_data['user_data'] = array_merge($user_data, $user_profile);

		$this->v('pages/checkout');
	}
	
	public function do_checkout() {
		$this->load->model(array('produkter', 'kunder_order'));
		$this->load->library(array('tank_auth', 'cart_handler'));
		$orders = $this->cart_handler->get_contents();

		// Compose order
		$this->kunder_order->id = null;
		$this->kunder_order->ipnummer = $this->input->ip_address();
		$this->kunder_order->datum = date('Y-m-d', strtotime('NOW'));
		$this->kunder_order->fnamn = $this->input->post('fnamn');
		$this->kunder_order->enamn = $this->input->post('enamn');
		$this->kunder_order->email = $this->input->post('email') ?  $this->input->post('email') : $this->tank_auth->get_email();
		$this->kunder_order->reklam = 0;
		$this->kunder_order->telefon = $this->input->post('telefon');
		$this->kunder_order->gadress = $this->input->post('gadress');
		$this->kunder_order->postnr = $this->input->post('postnr');
		$this->kunder_order->postort = $this->input->post('postort');
		$this->kunder_order->msg = $this->input->post('message');

		# Använd ett templat för att rendera ihop produkttabellen
		$prods_brief = "";
		foreach( $orders as $prod ){
			$totalprice = intval($prod['qty']) * floatval($prod['price']);
			$prods_brief .= $prod['name'] ." (" . $prod['qty'] ." st, ". $totalprice ." kr)<br/>";
		}
		$this->kunder_order->order = $prods_brief;
		
		if(
			# Try to send order confirmation and order notice mails
			$this->send_order_mail( $this->kunder_order, $orders ) &&
			$this->send_notice_mail( $this->kunder_order, $orders )
		){
			# If sent, save order and destroy cart and redirect to "thank you" page
			$this->kunder_order->save();
			$this->cart_handler->destroy_cart();

			# Save email in newsletter email, if it's not already there
			$email = $this->kunder_order->email;
				# verify it's not already there
			if( $this->db->where('mail',$email)->count_all_results('mail_adresser') === 0 ) {
				$added = $this->db->insert('mail_adresser', array('mail' => $email));
			}

			redirect('/bestallning-klar');
		}
	}
	
	public function send_order_mail( $order, $products ) {
		$this->load->library('email');
		
		$this->email->from('robot@fiskkillen.se', 'Fisk-killen.se');
		$this->email->to( $order->email );
		
		$this->email->subject('Beställning hos Fisk-killen.se');

		$order->product_table = $this->load->view('common/product-table', array(
			'products' => $products, 
			'class'=>'prod_table',
			'order'=>$order
		), TRUE);
		
		$msg = $this->load->view('email/order-confirmation', $order, TRUE);	

		$this->email->message( $msg );
		return $this->email->send();
	}

	public function send_notice_mail( $order, $products ) {
		$this->load->library('email');
		
		$this->email->from('order@fiskkillen.se', 'Beställning fr. webben');
		$this->email->to('info@fisk-killen.se'); 
		$this->email->cc('admin@twifyindustries.com');
		
		$this->email->subject('Fiskkillen beställning!');

		$order->product_table = $this->load->view('common/product-table', array(
			'products' => $products, 
			'class'=>'prod_table',
			'order'=>$order
		), TRUE);
		
		$msg = $this->load->view('email/order-notice', $order, TRUE);
		$this->email->message( $msg );
		return $this->email->send();
	}

	private function load_standards(){

		$sliderdata = array('products' => $this->produkter->getSliderProducts());	
		$this->view_data['slider_html'] = $this->load->view('pages/slider', $sliderdata, TRUE);
		$this->view_data['widgets'] = array();

		$this->load->library("cart_handler");
		$cart_data = array(
			'summary'	=> $this->cart_handler->get_count(),
			'price'		=> $this->cart_handler->get_total(),
			'display_css' => $this->cart_handler->get_count() > 0 ? "show" : "hide"
		);
		$this->view_data['cart_html'] = $this->load->view('widgets/cart', $cart_data, TRUE);
	}

	private function obj_to_array( $o ){
		$arr = array();
		foreach($o as $k=>$v){
			$arr[$k] = $v;
		}
		return $arr;
	}
}